﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Atha
{
	public class TestScriptDirectoryReader : ITestScriptsReader
	{
		private readonly string _directoryPath;

		private readonly bool _includeChildDirectories;

		/// <summary>
		/// Initializes a new instance of the <see cref="TestScriptDirectoryReader"/> class.
		/// </summary>
		/// <param name="directoryPath">The directory path.</param>
		/// <param name="includeChildDirectories">if set to <c>true</c> [include child directories].</param>
		public TestScriptDirectoryReader(string directoryPath, bool includeChildDirectories)
		{
			// TODO: Verify directory path

			this._directoryPath = directoryPath;
			this._includeChildDirectories = includeChildDirectories;
		}

		/// <summary>
		/// Gets the scripts.
		/// </summary>
		/// <returns></returns>
		public void GetScripts(Action<ITestScript[]> callback)
		{
			var directoryFilePaths = Directory.GetFiles(this._directoryPath, "*.*", (this._includeChildDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly));

			var scripts = new List<ITestScript>();

			foreach (var filePath in directoryFilePaths)
			{
				// okay dependency on TestScriptFileRepository
				var fileRepository = new TestScriptFileReader(filePath);
				fileRepository.GetScript(fileScript => scripts.Add(fileScript));
			}

			callback(scripts.ToArray());
		}
	}
}
